package com.face.sql.jsu;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;

import com.mchange.v2.c3p0.ComboPooledDataSource;  

import java.beans.PropertyVetoException;  
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;  
import java.sql.SQLException;    
import java.util.Properties;
/*
 * 1，为什么使用静态代码块
 * 2，为什么使用静态方法
 * 3，为什么要按照这种顺序关闭
 */

public class JDBCUtil {
	
	public static String driver ;
    public static String url ; 
    public static String username ;  
    public static String password ;  
  
    private static final JDBCUtil instance = new JDBCUtil();  
    private static ComboPooledDataSource comboPooledDataSource;  
  
    static {  
    	InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("/db.properties");  
        Properties prop = new Properties();
        try {  
            try {  
            	prop.load(in); 
    			driver = prop.getProperty("driver");
    			url = prop.getProperty("url");
                username = prop.getProperty("username");  
                password = prop.getProperty("password");
                Class.forName(driver);  
            } catch (ClassNotFoundException e) {  
                e.printStackTrace();  
            }  catch(IOException e){
            	e.printStackTrace();
            }
            comboPooledDataSource = new ComboPooledDataSource();  
            comboPooledDataSource.setDriverClass(driver);  
            comboPooledDataSource.setJdbcUrl(url);  
            comboPooledDataSource.setUser(username);  
            comboPooledDataSource.setPassword(password);  
            //下面是设置连接池的一配置  
            comboPooledDataSource.setMaxPoolSize(20);  
            comboPooledDataSource.setMinPoolSize(5);  
        } catch (PropertyVetoException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public synchronized static Connection getConnection() {  
        Connection connection = null;  
        try {  
            connection = comboPooledDataSource.getConnection();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } finally {  
            return connection;  
        }  
    }  
  
    private JDBCUtil() {
    	
    }  
  
    public static JDBCUtil getInstance() {  
        return instance;  
    }
	
    /*
	 * 关闭连接，按照结果集，stmt，con的顺序关闭
	 */
	public static void CloseConnection(ResultSet rs, PreparedStatement pstmt, Connection con){
		try {
			if(rs != null){
			rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(pstmt != null){
			try{
				pstmt.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(con != null){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
   /*
    * 未使用连接池的方法
	static String driver;
	static String url;
	static String username;
	static String password;
	static{
		
		 // 使用静态代码块可以在启动的时候运行
		InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("/db.properties");  
        Properties prop = new Properties();
		try {
			prop.load(in); 
			driver = prop.getProperty("driver");
			url = prop.getProperty("url");
            username = prop.getProperty("username");  
            password = prop.getProperty("password");
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	//提供一个连接的方法
	public static Connection getConnection(){
		Connection conn = null;
		try {
			//连接指定数据库，数据库地址，账号，密码
			conn = DriverManager.getConnection(url+"?useUnicode=true&characterEncoding=utf-8",username,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
    */
}
